package com.apestech.framework.util;

import com.apestech.formwork.xml.core.NodeVar;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.DataDictionary;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.modul.SqlCallVar;
import com.apestech.framework.xml.XmlSysConfig;

import java.io.File;

public class LocalVariable {

	private static boolean SYS_SINGLE_INS=false;
	private static String ROOT_DIR="d:\\";
	private static String TEMP_DIR="d:\\";
	private static String WORKFLOW_DIR="d:\\";
	private static String CONDITION_DIR="d:\\";
	private static JSONObject SYS_URL=new JSONObject();
	private static SqlCallVar SYS_QUERY=new SqlCallVar();
	private static XmlSysConfig config;
	private static JSONObject SYS_DB_TABLE_REMARK=new JSONObject();
	private static String PRO_FILES;

	public static void setDefaultXmlDir(String defaultXmlDir) {
		NodeVar.setDefaultXmlDir(defaultXmlDir);
	}

	public static String getTempDir() {
		return TEMP_DIR;
	}

	public static void setTempDir(String tempDir) throws Exception {
		TEMP_DIR = tempDir;
	}

	public static String getWorkflowDir() {
		return WORKFLOW_DIR;
	}

	public static void setWorkflowDir(String workflowDir) throws Exception {
		WORKFLOW_DIR =workflowDir;
	}

	public static String getConditionDir() {
		return CONDITION_DIR;
	}

	public static void setConditionDir(String conditionDir) throws Exception {
		CONDITION_DIR = conditionDir;
	}

	public static void setSysUrl(String sysid, String url){
		SYS_URL.put(sysid, url);
	}

	public static String getSysUrl(String sysid){
		return SYS_URL.getString(sysid);
	}

	public static SqlCallVar getSysQuery() {
		return SYS_QUERY;
	}

	public static void setSysQuery(SqlCallVar sysQuery) {
		SYS_QUERY = sysQuery;
	}

	public static String getRootDir() {
		return ROOT_DIR;
	}

	public static void setRootDir(String rootDir) {
		ROOT_DIR = rootDir;
	}

	public static JSONObject getSysDbTableRemark(SqlSession session) throws Exception {
		if(SYS_DB_TABLE_REMARK.get(session.getDbName())==null){
			SYS_DB_TABLE_REMARK.put(session.getDbName(), session.getAllTable());
		}
		return (JSONObject) SYS_DB_TABLE_REMARK.get(session.getDbName());
	}

	public static void setDefaultDir(String dir) throws Exception {
		String fileSep= File.separator;
		dir=dir.replace("/",fileSep);
		dir=dir.replace("\\",fileSep);
		int len=dir.length();
		if(!fileSep.equals(dir.substring(len-1, len))) dir=dir+File.separator;
		setWorkflowDir(dir+fileSep+"form"+fileSep);
		dir=dir+"query"+fileSep;
		setRootDir(dir);
		setDefaultXmlDir(dir);  /** 读取外部配置文件 **/
		setTempDir(dir+"temp"+fileSep);
		setConditionDir(dir+"condition"+fileSep);
		DataDictionary.setDefaultDir(dir+"dict"+fileSep);
		FileUtil.deleteDir(dir+"dict"+fileSep);
	}
	public static XmlSysConfig getConfig() throws Exception {
		if(config==null) config=new XmlSysConfig();
		return config;
	}

	public static String getProFiles() {
		return PRO_FILES;
	}

	public static void setProFiles(String proFiles) {
		int pos=proFiles.indexOf(".");
		if(pos>=0){
			proFiles=proFiles.substring(0, pos);
		}
		PRO_FILES = proFiles;
	}
}
